<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Xml.pb</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font><font color="#006666">;   PureBasic - Xml
</font><font color="#006666">;
</font><font color="#006666">;    (c) Fantaisie Software
</font><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font>
<font color="#924B72">#Window</font>     = 0
<font color="#924B72">#TreeGadget</font> = 0
<font color="#924B72">#XML</font>        = 0

<font color="#006666">; This procedure fills our TreeGadget, by adding the current node
</font><font color="#006666">; and then exploring all childnodes by recursively calling itself.
</font><font color="#006666">;
</font><b><font color="#006666">Procedure</font></b> <font color="#006666">FillTree</font>(*CurrentNode, CurrentSublevel)

  <font color="#006666">; Ignore anything except normal nodes. See the manual for
</font>  <font color="#006666">; XMLNodeType() for an explanation of the other node types.
</font>  <font color="#006666">;
</font>  <b><font color="#006666">If</font></b> <font color="#006666">XMLNodeType</font>(*CurrentNode) = <font color="#924B72">#PB_XML_Normal</font>
  
    <font color="#006666">; Add this node to the tree. Add name and attributes
</font>    <font color="#006666">;
</font>    Text$ =<font color="#006666"> GetXMLNodeName</font>(*CurrentNode) + " (Attributes: "
        
    <b><font color="#006666">If</font></b> <font color="#006666">ExamineXMLAttributes</font>(*CurrentNode)
      <b><font color="#006666">While</font></b> <font color="#006666">NextXMLAttribute</font>(*CurrentNode)
        Text$ +<font color="#006666"> XMLAttributeName</font>(*CurrentNode) + "=" +<font color="#006666"> Chr</font>(34) +<font color="#006666"> XMLAttributeValue</font>(*CurrentNode) +<font color="#006666"> Chr</font>(34) + " "
      <b><font color="#006666">Wend</font></b>
    <b><font color="#006666">EndIf</font></b>
    
    Text$ + ")"
    
<font color="#006666">    AddGadgetItem</font>(<font color="#924B72">#TreeGadget</font>, -1, Text$, 0, CurrentSublevel)
        
        
    <font color="#006666">; Now get the first child node (if any)
</font>    <font color="#006666">;    
</font>    *ChildNode =<font color="#006666"> ChildXMLNode</font>(*CurrentNode)
    
    <font color="#006666">; Loop through all available child nodes and call this procedure again
</font>    <font color="#006666">;
</font>    <b><font color="#006666">While</font></b> *ChildNode &lt;&gt; 0
<font color="#006666">      FillTree</font>(*ChildNode, CurrentSublevel + 1)      
      *ChildNode =<font color="#006666"> NextXMLNode</font>(*ChildNode)
    <b><font color="#006666">Wend</font></b>        
  
  <b><font color="#006666">EndIf</font></b>

<b><font color="#006666">EndProcedure</font></b>

FileName$ =<font color="#006666"> OpenFileRequester</font>("Choose XML file...", "", "XML files (*.xml)|*.xml|All files (*.*)|*.*", 0)
<b><font color="#006666">If</font></b> FileName$ &lt;&gt; ""

  <b><font color="#006666">If</font></b> <font color="#006666">LoadXML</font>(<font color="#924B72">#XML</font>, FileName$)

    <font color="#006666">; Note: 
</font>    <font color="#006666">;   The LoadXML() succeed if the file could be read. This does not mean that
</font>    <font color="#006666">;   there was no error in the XML though. To check this, XMLStatus() can be
</font>    <font color="#006666">;   used.
</font>    <font color="#006666">;
</font>    <font color="#006666">; Display an error message if there was a markup error
</font>    <font color="#006666">;
</font>    <b><font color="#006666">If</font></b> <font color="#006666">XMLStatus</font>(<font color="#924B72">#XML</font>) &lt;&gt; <font color="#924B72">#PB_XML_Success</font>
      Message$ = "Error in the XML file:" +<font color="#006666"> Chr</font>(13)
      Message$ + "Message: " +<font color="#006666"> XMLError</font>(<font color="#924B72">#XML</font>) +<font color="#006666"> Chr</font>(13)
      Message$ + "Line: " +<font color="#006666"> Str</font>(<font color="#006666">XMLErrorLine</font>(<font color="#924B72">#XML</font>)) + "   Character: " +<font color="#006666"> Str</font>(<font color="#006666">XMLErrorPosition</font>(<font color="#924B72">#XML</font>))
<font color="#006666">      MessageRequester</font>("Error", Message$)
    <b><font color="#006666">EndIf</font></b>
    
    <font color="#006666">; Note:
</font>    <font color="#006666">;   Even if there was an error in the XML, all nodes before the error position
</font>    <font color="#006666">;   are still accessible, so open the window and show the tree anyway.
</font>    <font color="#006666">;
</font>    <b><font color="#006666">If</font></b> <font color="#006666">OpenWindow</font>(<font color="#924B72">#Window</font>, 0, 0, 500, 500, "XML Example", <font color="#924B72">#PB_Window_SystemMenu</font>|<font color="#924B72">#PB_Window_ScreenCentered</font>)
<font color="#006666">      TreeGadget</font>(<font color="#924B72">#TreeGadget</font>, 10, 10, 480, 480)
      
      <font color="#006666">; Get the main XML node, and call the FillTree() procedure with it
</font>      <font color="#006666">;
</font>      *MainNode =<font color="#006666"> MainXMLNode</font>(<font color="#924B72">#XML</font>)      
      <b><font color="#006666">If</font></b> *MainNode
<font color="#006666">        FillTree</font>(*MainNode, 0)
      <b><font color="#006666">EndIf</font></b>
      
      <font color="#006666">; Expand all nodes for a nicer view
</font>      <font color="#006666">;
</font>      <b><font color="#006666">For</font></b> i = 0 <b><font color="#006666">To</font></b> <font color="#006666">CountGadgetItems</font>(<font color="#924B72">#TreeGadget</font>) - 1
<font color="#006666">        SetGadgetItemState</font>(<font color="#924B72">#TreeGadget</font>, i, <font color="#924B72">#PB_Tree_Expanded</font>)
      <b><font color="#006666">Next</font></b> i
      
      <font color="#006666">; Wait for the window close event.
</font>      <font color="#006666">;
</font>      <b><font color="#006666">Repeat</font></b>
        Event =<font color="#006666"> WaitWindowEvent</font>()
      <b><font color="#006666">Until</font></b> Event = <font color="#924B72">#PB_Event_CloseWindow</font>
    <b><font color="#006666">EndIf</font></b>
        
  <b><font color="#006666">Else</font></b>
<font color="#006666">    MessageRequester</font>("Error", "The file cannot be opened.")
  <b><font color="#006666">EndIf</font></b>

<b><font color="#006666">EndIf</font></b>
</font></pre>
</body></html>
